
export const isAndroid = uni.getDeviceInfo().platform == "android"; //android终端
export const isiOS = uni.getDeviceInfo().platform == "ios"; //ios终端
export const isHarmony = uni.getDeviceInfo().platform == 'ohos'; // 鸿蒙系统
export const isDevtools = uni.getDeviceInfo().platform == 'devtools'; // 模拟器
export const REG_USER_NAME = /^[\u4E00-\u9FA5a-zA-Z0-9_-]{4,16}$/;
export const REG_PHONE =
  /^[1](([3][0-9])|([4][01456789])|([5][012356789])|([6][2567])|([7][0-8])|([8][0-9])|([9][012356789]))[0-9]{8}$/;
export const REG_URL =
  /(((^https?:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export const emojiReg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;
export const pureTextReg = /[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD83C-\uDBFF][\uDC00-\uDFFF]|[\u2600-\u27BF]/g;
export const hasChinese = /[\u4e00-\u9fa5]/g

export function filterChars(str: string) {
  if (str) {
    return str.replace(/[^a-zA-Z\d\u4e00-\u9fa5\.@-]+/gi, '');
  }
  return '';
}

export function isMobile(val: string) {
  return /^1[3456789]\d{9}$/.test(val);
}

export function isHttp(url: string) {
  return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
}

export function isExternal(path: string) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

export function validURL(url: string) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
}

export function validEmail(email: string) {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
}

export function isNumber(checkVal: string) {
  var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
  return reg.test(checkVal);
}

export function isAbsoluteURL(url: string) {
  // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
  // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
  // by any combination of letters, digits, plus, period, or hyphen.
  return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
}

export function uuid() {
  const s: any = [];
  const hexDigits = "0123456789abcdef";
  for (let i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  const uuid = s.join("");
  return uuid;
}

export function isFirstPage() {
  const pages = getCurrentPages();
  return pages.length === 1;
}

export function dateFormat(val: number | string | Date, fmt: string = "yyyy-MM-dd") {
  if (typeof val == 'string') {
    if (val.includes('-')) {
      val = val.replace(/-/g, '/');
    }
    if (!val.includes('/')) {
      val = parseInt(val);
    }
  }
  const date = new Date(val);
  var o: any = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    S: date.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}

export function jsonParseItem(name: string, parse: boolean = true): any {
  const data = uni.getStorageSync(name);
  if (!data || data == "null" || data == "undefined") {
    return null;
  }
  if (parse) {
    try {
      return JSON.parse(data);
    } catch (e: any) {
      return data;
    }
  } else {
    return data;
  }
}

export function showToast(title, success?) {
  uni.showToast({
    title: title,
    icon: "none",
    duration: 2000,
    success: function () {
      typeof success === "function" && success();
    },
  });
}

export function validatePassword(pwd: any) {
  const hasNumber = /\d+/;
  const hasLetter = /[a-zA-Z]+/;
  const startLetter = /^[a-zA-Z]/;
  // 特殊字符右后端检验
  // const hasSpecial = /[~!@#$%^&*)(_+}{|:?><\[\]`\-=,\\\'\"\.]+/;
  return hasNumber.test(pwd) && hasLetter.test(pwd) && startLetter.test(pwd);
}

export function isNumberType(obj: any): boolean {
  return typeof obj === "number" && !isNaN(obj);
}

export function isStringType(obj: any): boolean {
  return typeof obj === "string";
}

export function isValidNumber(value: string): boolean {
  return /^-?\d+(\.\d+)?$/.test(value);
}

const mobileRegex = /(\d{3})\d{4}(\d{4})/;
export function encryptMobile(mobile: string): string {
  const tel = "" + mobile;
  return tel.replace(mobileRegex, "$1****$2")
}

export function fakeRequest(second: number = 3) {
  return new Promise((resovle) => {
    setTimeout(() => {
      resovle(true)
    }, second * 1000);
  })
}